"""
Case Type   : xml类型
Case Name   : 验证JDBC支持对xml类型进行select，update，insert，delete
Create At   : 2022/8/29
Owner       : songjing20
Description :
    1.执行java脚本，对表xml类型字段进行select，update，insert，delete
    2.环境清理，恢复参数
Expect      :
    1.执行成功
    2.成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class XmlDatatype(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_root = Node('PrimaryRoot')
        self.pri_dbuser = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common('PrimaryRoot')
        self.java_file = "Opengauss_Function_Datatype_Xml_Case0054.java"
        self.target_path = os.path.join(macro.DB_BACKUP_PATH, 'JDBC_xml_0054')
        self.jar_abspath = os.path.join(self.target_path, 'postgresql.jar')
        cmd = f"if [ -d {self.target_path} ];" \
              f"then rm -rf {self.target_path};fi; mkdir -p {self.target_path}"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.pri_root, cmd)
        self.assertEqual('', res.strip(), '预置目录失败')

    def test_xml_datatype(self):
        text = "-----step1:执行java脚本，对表xml类型字段进行select，update，" \
               "insert，delete;expect:执行成功-----"
        self.log.info(text)
        self.log.info('-----复制java脚本至指定的主机路径-----')
        self.pri_root.scp_put(os.path.join(macro.SCRIPTS_PATH, self.java_file),
                              self.target_path)
        self.log.info('-----复制jdbc jar包至指定的主机路径-----')
        self.pri_root.scp_put(macro.JDBC_PATH, self.jar_abspath)
        cmd = f'ls {self.target_path}'
        self.log.info(cmd)
        res = self.pri_root.sh(cmd).result()
        self.log.info(res)
        self.assertIn(self.java_file, res, f'复制java脚本失败:{text}')
        self.assertIn('postgresql.jar', res, f'复制jar包失败:{text}')
        self.log.info('-----编译java文件-----')
        cmd = f"cd {self.target_path};" \
              f"javac -encoding utf-8 -cp {self.jar_abspath} {self.java_file}"
        self.log.info(cmd)
        compile_res = self.pri_root.sh(cmd).result()
        self.log.info(compile_res)
        self.assertNotIn('ERROR', compile_res, f'编译失败:{text}')
        self.log.info('-----运行java预编译代码-----')
        para = f"{self.pri_dbuser.db_host}:{self.pri_dbuser.db_port}" \
               f" {self.pri_dbuser.db_name} {self.pri_dbuser.db_user}" \
               f" {self.pri_dbuser.db_password}"
        java_cmd = f"cd {self.target_path};" \
                   f"java -cp {self.jar_abspath}:{self.target_path} " \
                   f"{self.java_file[:-5]} {para} "
        self.log.info(java_cmd)
        java_res = self.pri_root.sh(java_cmd).result()
        self.log.info(java_res)
        self.assertIn('''1 | asdf | asdddf
123 | null | <book>《孙子兵法》</book>
select result after update:
1 | <update>testcolumn</update> | asdddf
123 | null | update values
Delete success''', java_res, f'执行失败:{text}')

    def tearDown(self):
        text = "-----step5:环境清理;expect:成功-----"
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            f"drop table if exists t_xml_0054 cascade;")
        self.log.info(drop_res)
        cmd = f"rm -rf {self.target_path};"
        self.log.info(cmd)
        cmd_res = self.common.get_sh_result(self.pri_root, cmd)
        self.assertEqual('', cmd_res, f'删除目录失败:{text}')
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, drop_res,
                      f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
